#pragma once

#include"common.hpp"
const std::string rubbish  = "/dev/null"


void Daemon(int nochdir, int noclose)
{
    signal(SIGPIPE,SIG_IGN);
    signal(SIGCHLD,SIG_IGN);

    if(fork() > 0)
        exit(SUCCESS);
    
    setsid();

    if(nochdir == 0){
        chdir("/");
    }

    if(noclose == 0){
        int fd = open(rubbish.c_str(),O_RDWR);
        if(fd < 0){
            LOG(level::ERROR)<<"open "<< rubbish << " false";
            exit(OPEN_EER);
        }else
        {
            dup2(fd,0);
            dup2(fd,1);
            dup2(fd,2);
            close(fd);
        }
    }
    
}